package parse

import (
	"Crawlab/common/engine/os"
	"regexp"
)

const cityRegexp = `<h2 class="post-title"><a href="(http://localhost:8080/mock/album.zhenai.com/u/[0-9]+)">([^<]+)</a></h2>`

func CityParseResult(content []byte) os.ParseResult {
	compile := regexp.MustCompile(cityRegexp)
	// 参数1: 被正则匹配的内容, []byte.
	// 参数2: 匹配个数, -1 全部匹配, int.
	matchArray := compile.FindAllSubmatch(content, -1)

	// 准备解析结果对象.
	parseResult := os.ParseResult{}

	// 从解析器的匹配内容拼接解析结果对象.
	for _, singleArray := range matchArray {
		// 数组内容: [完整连接, 连接, 用户名]

		name := singleArray[2]
		parseResult.Items = append(parseResult.Items, "User "+string(name))
		parseResult.Requests = append(parseResult.Requests, os.Request{
			Url: string(singleArray[1]),
			ParseFunc: func(content []byte) os.ParseResult {
				return UserParseResult(content, string(name))
			},
		})
	}

	return parseResult
}
